package com.frank.loadinglibrary.special.dot;

import com.frank.loadinglibrary.common.BaseLoadingComponent;
import com.frank.loadinglibrary.special.dot.anim.TextAnimator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.app.Context;

/**
 * @author SUQI
 * @date 2021/5/25
 * @description
 **/
public class DotLoadingComponent extends BaseLoadingComponent<DotLoadingComponent> implements Component.DrawTask {

    public static int DEFAULT_WIDTH = 200;
    public static int EXTRA = DEFAULT_WIDTH / (TextAnimator.STR.length() - 2);
    public static int DEFAULT_HEIGHT = DEFAULT_WIDTH + EXTRA;

    private Paint mPaint;


    private Controller mController;
    private boolean isInit = false;


    public DotLoadingComponent(Context context) {
        this(context, null);
    }

    public DotLoadingComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public DotLoadingComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init();
        addDrawTask(this);
        setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                mController = new Controller(component);
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
    }

    private void init() {
        if (isInit) {
            return;
        }
        isInit = true;
        // create Controller


        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setStrokeWidth(4);
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void stopAnimators() {

    }

    @Override
    protected void startAnimators() {

    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (null != mController) {
            mController.draw(canvas, mPaint);
        }
    }

}
